package com.cskaoyan.order.service.impl;

import com.cskaoyan.order.converter.OrderConverter;
import com.cskaoyan.order.dal.entitys.Order;
import com.cskaoyan.order.dal.entitys.OrderItem;
import com.cskaoyan.order.dal.entitys.OrderShipping;
import com.cskaoyan.order.dal.persistence.OrderItemMapper;
import com.cskaoyan.order.dal.persistence.OrderMapper;
import com.cskaoyan.order.dal.persistence.OrderShippingMapper;
import com.cskaoyan.order.dto.*;
import com.cskaoyan.order.form.OrderListRequest;
import com.cskaoyan.order.service.OrderQueryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

@Service
public class OrderQueryServiceImpl implements OrderQueryService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderItemMapper orderItemMapper;
    @Autowired
    OrderShippingMapper orderShippingMapper;
    @Autowired
    OrderConverter orderConverter;


    @Override
    public OrderListResponse orderList(OrderListRequest request) {
        OrderListResponse orderListResponse = new OrderListResponse();
        //TODO 查询该用户的所有订单数据，根据id
        PageHelper.startPage(request.getPage(), request.getSize());
        Example example = new Example(Order.class);
        example.setOrderByClause("create_time desc");
        example.createCriteria()
                .andEqualTo("userId",request.getUserId());
        List<Order> orders = orderMapper.selectByExample(example);
        PageInfo<Order> orderPageInfo = new PageInfo<>(orders);
        long total = orderPageInfo.getTotal();
        orderListResponse.setTotal(total);
        //TODO 类型的转换获取返回的info
        ArrayList<OrderDetailInfo> list = new ArrayList<>();

        for (Order order : orders) {
            OrderDetailInfo orderDetailInfo = orderConverter.order2detail(order);
            //TODO huoqu 获取里边的商品信息
            Example example1 = new Example(OrderItem.class);
            example1.createCriteria()
                    .andEqualTo("orderId",order.getOrderId());
            List<OrderItem> orderItems = orderItemMapper.selectByExample(example1);
            List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItems);
            orderDetailInfo.setOrderItemDto(orderItemDtos);
            //TODO 邮寄的地址
            OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(order.getOrderId());
            OrderShippingDto orderShippingDto = orderConverter.shipping2dto(orderShipping);
            orderDetailInfo.setOrderShippingDto(orderShippingDto);
            list.add(orderDetailInfo);
        }
        orderListResponse.setData(list);
        return orderListResponse;
    }

    /**
    * @description: 查看订单的详情
    [orderId]
    * @return: com.cskaoyan.order.dto.OrderDetailResponse
    * @author: 杨庆
    * @time: 2022.9.21 10:01
     */
    @Override
    public OrderDetailResponse orderDetail(String orderId) {
        //TODO 根据订单的编号查询订单的信息
        Order order = orderMapper.selectByPrimaryKey(orderId);
        OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(orderId);
        OrderDetailResponse orderDetailResponse1 = orderConverter.order2res(order, orderShipping);
        //TODO 查看商品的信息
        Example example = new Example(OrderItem.class);
        example.createCriteria()
                .andEqualTo("orderId",orderId);
        List<OrderItem> orderItems = orderItemMapper.selectByExample(example);
        List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItems);
        orderDetailResponse1.setGoodsList(orderItemDtos);
        return orderDetailResponse1;
    }
}
